package com.second.app.excutor;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author GYZ
 * @Date 2024/1/26
 * <pre>
 *
 * </pre>
 */
public class ThreadExecutorAPI {
}
//只要调用shutDown()方法，isShutDown方法的返回值就是true
class IsShutDown {

    public static void main(String[] args) {
        Runnable runnable = () -> {
            try {
                System.out.println("打印了！ begin" + Thread.currentThread().getName());
                Thread.sleep(1000);
                System.out.println("打印了！   end " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                2,
                2,
                Integer.MAX_VALUE,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>());

        executor.execute(runnable);
        System.out.println("A=" + executor.isShutdown());

        executor.shutdown();
        System.out.println("B=" + executor.isShutdown());
    }
}

//如果正在执行的程序处于shutDown或shutDownNow之后处于正在终止但尚未完全终止的过程中，调用isTerminating()
//则返回true,可以比喻成，门是否正在关闭。门彻底关闭时，线程池就关闭了。如果线程池关闭后，也就是所有任务都已经完成
//则isTerminated()返回true,此方法可以理解为是否已经关闭。
class IsTerminal{
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            try {
                System.out.println("打印了！ begin" + Thread.currentThread().getName());
                Thread.sleep(1000);
                System.out.println("打印了！   end " + Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2,
                9999,
                9999L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>());

        pool.execute(runnable);
        pool.execute(runnable);
        System.out.println(pool.isTerminated()+" "+pool.isTerminating());
        pool.shutdown();
        Thread.sleep(1000);
        System.out.println(pool.isTerminated()+" "+pool.isTerminating());
        Thread.sleep(1000);
        System.out.println(pool.isTerminated()+" "+pool.isTerminating());
    }
}
//pool.awaitTermination(10,TimeUnit.SECONDS)
//从打印的结果来看，main end 打印的时间是线程执行完闭后的时间，也就是awaitTermination()被执行时，如果池中
//有任务在被执行，则调用awaitTermination()方法出现阻塞，等待指定的时间，
//如果没有任务时，则不在阻塞。
class AwaitTermination{
    public static void main(String[] args) throws InterruptedException {
        Runnable runnable = () -> {
            try {
                System.out.println(Thread.currentThread().getName()+" "+System.currentTimeMillis());
                Thread.sleep(4000);
                System.out.println(Thread.currentThread().getName()+" "+System.currentTimeMillis());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                2,
                9999,
                9999L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>());

        pool.execute(runnable);

        System.out.println("main begin!"+System.currentTimeMillis());
        System.out.println(pool.awaitTermination(10,TimeUnit.SECONDS));
        System.out.println("main end!"+System.currentTimeMillis());
    }
}
